<?php
/**
 * Created by PhpStorm.
 * User: huangpeng3
 * Date: 2018/1/28
 * Time: 下午3:22
 */

namespace Home\Model;

use Think\Model;


class ExchangeModel extends Model
{

    protected $tableName = 'user_exchange';


    /**
     * @param $uid
     * @param $eid
     * @param $aKey
     * @param $sKey
     * @param $label
     */
    public function addExchange($uid, $eid, $aKey, $sKey, $label)
    {
        $data = array(
            'uid' => $uid,
            'eid' => $eid,
            'akey' => $aKey,
            'skey' => $sKey,
            'label' => $label
        );
        if (empty($this->where($data)->select())) {
            if ($this->add($data)) {
                return 200;
            } else {
                return -1;
            }
        } else {
            return -2;
        }
    }

    public function delExchange($pid, $uid, $eid)
    {
        $data = array(
            'id' => $pid,
            'uid' => $uid,
            'eid' => $eid
        );
        if ($this->where($data)->delete()) {
            return 200;
        } else {
            return -1;
        }
    }

    public function updateExchange($pid, $uid, $eid, $akey, $skey, $label)
    {
        $where = array(
            'id' => $pid,
            'uid' => $uid
        );
        $data = array(
            'eid' => $eid,
            'akey' => $akey,
            'skey' => $skey,
            'label' => $label
        );
        if (!empty($this->where($where)->select())) {
            if ($this->where($where)->save($data)) {
                return 200;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }

    public function getAll($uid)
    {
        $rst = $this->where('uid=' . $uid)->select();
        return $rst;
    }

}